import java.io.IOException;

import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

//import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

public class Amostragem {

	public static class Map extends MapReduceBase implements
			Mapper<LongWritable, Text, Text, Text> {
		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(LongWritable key, Text value,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			try {
				String[] line = value.toString().split(",");
				if (line[2].equals("1") || line[2].equals("2")) {
					word.set(value.toString());
					output.collect(word, new Text("-"));
				}
			} catch (Exception e) {

			}

		}
	}

	public static class Reduce extends MapReduceBase implements
			Reducer<Text, Text, Text, Text> {
		private Text word = new Text();

		public void reduce(Text key, Iterator<Text> values,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			word.set(key.toString());
			output.collect(word, new Text(""));
		}
	}

	public static void main(String[] args) throws Exception {

		JobConf conf = new JobConf(Amostragem.class);
		// conf.setNumReduceTasks(10);
		conf.setJobName("Amostragem");
		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(Text.class);
		conf.setMapperClass(Map.class);

		conf.setCombinerClass(Reduce.class);

		conf.setReducerClass(Reduce.class);
		conf.setInputFormat(TextInputFormat.class);
		conf.setOutputFormat(TextOutputFormat.class);
		FileInputFormat.setInputPaths(conf, new Path(args[0]));
		FileOutputFormat.setOutputPath(conf, new Path(args[1]));

		JobClient.runJob(conf);

	}
}
